<template>
  <div class="home-container">
    <!-- 轮播图区域 -->
    <div class="banner">
      <el-carousel height="280px" indicator-position="outside">
        <el-carousel-item v-for="(item, index) in banners" :key="index">
          <div class="banner-item" :style="`background-image: url(${item.img})`">
            <div class="banner-content">
              <h2>{{ item.title }}</h2>
              <p>{{ item.desc }}</p>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 快捷操作区 -->
    <div class="quick-actions">
      <el-row :gutter="20">
        <el-col :xs="12" :sm="8" :md="6">
          <div class="action-card" @click="$router.push('/user/order/create')">
            <i class="el-icon-plus"></i>
            <span>预约洗衣</span>
          </div>
        </el-col>
        <el-col :xs="12" :sm="8" :md="6">
          <div class="action-card" @click="$router.push('/user/order/list')">
            <i class="el-icon-s-order"></i>
            <span>我的订单</span>
          </div>
        </el-col>
        <el-col :xs="12" :sm="8" :md="6">
          <div class="action-card" @click="$router.push('/user/center')">
            <i class="el-icon-user"></i>
            <span>个人中心</span>
          </div>
        </el-col>
        <el-col :xs="12" :sm="8" :md="6">
          <div class="action-card" @click="$router.push('/user/promotions')">
            <i class="el-icon-medal"></i>
            <span>优惠活动</span>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 公告和新闻区域 -->
    <div class="info-section">
      <el-row :gutter="20">
        <!-- 公告信息 -->
        <el-col :sm="24" :md="12">
          <el-card class="info-card">
            <div slot="header" class="card-header">
              <span>最新公告</span>
              <el-button style="float: right; padding: 3px 0" type="text" @click="seeMoreAnnouncements">
                查看更多
              </el-button>
            </div>
            <div v-if="announcements.length === 0" class="empty-data">
              暂无公告信息
            </div>
            <div v-else class="info-list">
              <div v-for="(item, index) in announcements" :key="index" class="info-item" @click="viewAnnouncementDetail(item)">
                <div class="info-title">{{ item.title }}</div>
                <div class="info-time">{{ formatDate(item.createTime) }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <!-- 新闻资讯 -->
        <el-col :sm="24" :md="12">
          <el-card class="info-card">
            <div slot="header" class="card-header">
              <span>新闻资讯</span>
              <el-button style="float: right; padding: 3px 0" type="text" @click="seeMoreNews">
                查看更多
              </el-button>
            </div>
            <div v-if="news.length === 0" class="empty-data">
              暂无新闻资讯
            </div>
            <div v-else class="info-list">
              <div v-for="(item, index) in news" :key="index" class="info-item" @click="viewNewsDetail(item)">
                <div class="info-title">{{ item.title }}</div>
                <div class="info-time">{{ formatDate(item.createTime) }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 服务介绍区 -->
    <div class="service-intro">
      <h2>我们的服务</h2>
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" v-for="(service, index) in services" :key="index">
          <div class="service-card">
            <div class="service-icon"><i :class="service.icon"></i></div>
            <h3>{{ service.name }}</h3>
            <p>{{ service.desc }}</p>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 消息通知组件 -->
    <message-notification class="floating-notification" />

    <!-- 公告详情弹窗 -->
    <el-dialog title="公告详情" :visible.sync="announcementDialogVisible" width="500px">
      <h3 v-if="currentAnnouncement">{{ currentAnnouncement.title }}</h3>
      <p v-if="currentAnnouncement" class="announcement-content">{{ currentAnnouncement.content }}</p>
      <p v-if="currentAnnouncement" class="announcement-time">发布时间：{{ formatDate(currentAnnouncement.createTime) }}</p>
    </el-dialog>

    <!-- 新闻详情弹窗 -->
    <el-dialog title="新闻详情" :visible.sync="newsDialogVisible" width="500px">
      <h3 v-if="currentNews">{{ currentNews.title }}</h3>
      <p v-if="currentNews" class="news-content">{{ currentNews.content }}</p>
      <p v-if="currentNews" class="news-time">发布时间：{{ formatDate(currentNews.createTime) }}</p>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import { formatDate } from '@/utils/date'
import MessageNotification from '@/components/MessageNotification.vue'

export default {
  name: 'UserHome',
  components: {
    MessageNotification
  },
  data() {
    return {
      banners: [
        { img: 'https://img.freepik.com/free-photo/laundry-basket-with-clean-clothes_144627-8599.jpg', title: '专业洗衣服务', desc: '为您提供专业的洗衣服务，让您的衣物焕然一新' },
        { img: 'https://img.freepik.com/free-photo/woman-doing-laundry_23-2148356901.jpg', title: '快速取送服务', desc: '便捷的取送服务，让您足不出户享受专业洗衣' },
        { img: 'https://img.freepik.com/free-photo/white-laundry-basket_23-2147915827.jpg', title: '环保清洁剂', desc: '使用环保清洁剂，呵护您的衣物和环境' }
      ],
      announcements: [],
      news: [],
      services: [
        { name: '标准洗涤', desc: '适合日常衣物的基础清洗服务，使用专业洗涤剂，确保衣物清洁干净。', icon: 'el-icon-s-shop' },
        { name: '高级洗护', desc: '针对贵重衣物的精细洗护服务，采用特殊工艺，维护衣物品质。', icon: 'el-icon-s-goods' },
        { name: '快速洗衣', desc: '急需使用的衣物快速处理，4小时内完成洗涤和烘干，方便您的紧急需求。', icon: 'el-icon-time' },
        { name: '家居清洗', desc: '提供床单、被罩、窗帘等家居织物的专业清洗，去除深层污垢。', icon: 'el-icon-house' },
        { name: '鞋类清洁', desc: '专业鞋类清洁服务，让您的鞋子焕然一新。', icon: 'el-icon-s-cooperation' },
        { name: '上门取送', desc: '提供便捷的上门取送服务，让您足不出户享受专业洗衣。', icon: 'el-icon-truck' }
      ],
      announcementDialogVisible: false,
      currentAnnouncement: null,
      newsDialogVisible: false,
      currentNews: null
    }
  },
  created() {
    this.fetchAnnouncements()
    this.fetchNews()
  },
  methods: {
    formatDate,
    async fetchAnnouncements() {
      try {
        const res = await axios.get('/api/announcement/list?pageSize=5').then(res => res.data)
        if (res.code === 200) {
          this.announcements = res.data.slice(0, 5)
        }
      } catch (error) {
        console.error('获取公告列表失败', error)
      }
    },
    async fetchNews() {
      try {
        const res = await axios.get('/api/news/list?pageSize=5').then(res => res.data)
        if (res.code === 200) {
          this.news = res.data.slice(0, 5)
        }
      } catch (error) {
        console.error('获取新闻列表失败', error)
      }
    },
    viewAnnouncementDetail(announcement) {
      this.currentAnnouncement = announcement
      this.announcementDialogVisible = true
    },
    viewNewsDetail(news) {
      this.currentNews = news
      this.newsDialogVisible = true
    },
    seeMoreAnnouncements() {
      // 可以添加查看更多公告的逻辑
      this.$message.info('查看更多公告功能即将上线')
    },
    seeMoreNews() {
      // 可以添加查看更多新闻的逻辑
      this.$message.info('查看更多新闻功能即将上线')
    }
  }
}
</script>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.banner {
  margin-bottom: 30px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.banner-item {
  height: 100%;
  background-size: cover;
  background-position: center;
  position: relative;
  display: flex;
  align-items: center;
}

.banner-content {
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 20px;
  max-width: 50%;
  margin-left: 5%;
  border-radius: 8px;
}

.banner-content h2 {
  margin-top: 0;
  margin-bottom: 10px;
  font-size: 24px;
}

.quick-actions {
  margin-bottom: 30px;
}

.action-card {
  background-color: #fff;
  height: 100px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.action-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px 0 rgba(0, 0, 0, 0.2);
}

.action-card i {
  font-size: 36px;
  color: #409EFF;
  margin-bottom: 10px;
}

.info-section {
  margin-bottom: 30px;
}

.info-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-list {
  max-height: 350px;
  overflow-y: auto;
}

.info-item {
  padding: 12px 0;
  border-bottom: 1px solid #eee;
  cursor: pointer;
  transition: background-color 0.3s;
}

.info-item:hover {
  background-color: #f9f9f9;
}

.info-title {
  font-size: 14px;
  margin-bottom: 5px;
  color: #303133;
}

.info-time {
  font-size: 12px;
  color: #909399;
}

.empty-data {
  padding: 30px 0;
  text-align: center;
  color: #909399;
}

.service-intro {
  margin-bottom: 30px;
}

.service-intro h2 {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
  padding-bottom: 15px;
}

.service-intro h2:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 50px;
  height: 3px;
  background-color: #409EFF;
  transform: translateX(-50%);
}

.service-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  text-align: center;
  height: 100%;
  min-height: 200px;
  margin-bottom: 20px;
  transition: all 0.3s;
}

.service-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px 0 rgba(0, 0, 0, 0.2);
}

.service-icon {
  font-size: 40px;
  color: #409EFF;
  margin-bottom: 10px;
}

.service-card h3 {
  color: #303133;
  margin-bottom: 10px;
}

.service-card p {
  color: #606266;
  font-size: 14px;
}

.floating-notification {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
}

.announcement-content,
.news-content {
  white-space: pre-line;
  line-height: 1.6;
  margin: 20px 0;
}

.announcement-time,
.news-time {
  color: #909399;
  font-size: 12px;
  text-align: right;
}

@media (max-width: 768px) {
  .banner-content {
    max-width: 80%;
  }
  
  .service-card {
    min-height: auto;
  }
}
</style> 